#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <stdbool.h>

#include <immintrin.h>

__m128i _mm_loadu_si128(__m128i const*mem_addr);    // 加载16字节,指令 movdqu
__m128i _mm_srai_epi32(__m128i a, int imm8);        // 32位整数算术右移，向负无穷取整, 指令: psrad xmm, imm8
void _mm_storeu_si128(__m128i *mem_addr, __m128i a); // 保存16字节, 指令 movdqu


void arithmetic_shift_right(int *p)
{
   int q1[8];
   int r1[8];
   int q2[8];
   int r2[8];
   
   printf("test int right shirt, use c code\n");
   for (int i=0;i<8;i++)
   {
      q1[i]=(p[i]>>7);
      printf("%d >> 7 =%d,\n",p[i],q1[i]);
   }
   
   __m128i r0,q;
   r0 = _mm_loadu_si128((__m128i *)p);
   q  = _mm_srai_epi32(r0, 7);
   _mm_storeu_si128((__m128i *)q2, q);

   r0 = _mm_loadu_si128((__m128i *)(p+4));
   q  = _mm_srai_epi32(r0, 7);
   _mm_storeu_si128((__m128i *)(q2+4), q);
   
   printf("test int right shift,use psrad\n");
   for (int i=0;i<8;i++)
   {
      printf("%d >> 7 =%d\n",p[i],q2[i]);
   }
}

void test_right_shift()
{
   printf("\nrun %s()\n",__func__);
   int ary[8]={-129,-128,-127,-1,1,127,128,129};
   arithmetic_shift_right(ary);
}

int main(int argc, char* argv[])
{
   test_right_shift();
   return 0;
}

